//
//  LKSettingViewController.m
//  LKIntelligentVideoProject
//
//  Created by she1323 on 2018/9/20.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKSettingViewController.h"
#import "AppDelegate.h"
#import "LKPushSetModel.h"
#import "LKResetSecrectViewController.h"

const CGFloat BackGroupHeight = 200;
const CGFloat HeadImageHeight= 80;


@interface LKSettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSArray *titlesArray;

@property(nonatomic,strong)UISwitch *noticeSwitch;

@property(nonatomic,strong)UISwitch *showHouseSwitch;


@end

@implementation LKSettingViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //头像设置
    self.headImageView.image = [UIImage imageNamed:@"myheadimage.jpeg"];
    //背景设置
    self.imageBG.image = [UIImage imageNamed:@"BG.jpg"];
    [self createUI];
}

- (void)createUI{
    self.automaticallyAdjustsScrollViewInsets=NO;
//    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    _tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    _tableView.contentInset = UIEdgeInsetsMake(BackGroupHeight, 0, 0, 0);
    
    [self.view addSubview:_tableView];
    
    [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    _imageBG = [[UIImageView alloc]init];
    _imageBG.frame = CGRectMake(0, -BackGroupHeight, WIDTH, BackGroupHeight);
    _imageBG.image = [UIImage imageNamed:@"BG.jpg"];
    
    [_tableView addSubview:_imageBG];
    //
    _BGView = [[UIView alloc]init];
    _BGView.backgroundColor=[UIColor clearColor];
    _BGView.frame=CGRectMake(0, -BackGroupHeight, WIDTH, BackGroupHeight);
    
    [_tableView addSubview:_BGView];
    
    //
    _headImageView=[[UIImageView alloc]init];
    _headImageView.image=[UIImage imageNamed:@"myheadimage.jpeg"];
    _headImageView.frame=CGRectMake((WIDTH-HeadImageHeight)/2, 40, HeadImageHeight, HeadImageHeight);
    _headImageView.layer.cornerRadius = HeadImageHeight/2;
    _headImageView.clipsToBounds = YES;
    
    
    [_BGView addSubview:_headImageView];
    
    //
    
    _nameLabel=[[UILabel alloc]init];
<<<<<<< HEAD
    _nameLabel.text=[LKUserInforTool account].loginAccount;
=======
    _nameLabel.text=@"测试员";
>>>>>>> parent of 2e14138... 提交
    _nameLabel.textAlignment=NSTextAlignmentCenter;
    _nameLabel.frame=CGRectMake((WIDTH-HeadImageHeight)/2, CGRectGetMaxY(_headImageView.frame)+5, HeadImageHeight, 20);

    [_BGView addSubview:_nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headImageView.mas_bottom).offset(10);
        make.centerX.equalTo(self.headImageView.mas_centerX);
    }];
    _titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    _titleLabel.textColor=[UIColor whiteColor];
    _titleLabel.text=@"title";
    
    _titleLabel.textAlignment=NSTextAlignmentCenter;
    
    self.navigationItem.titleView=_titleLabel;
    _titleLabel.alpha=0;
    
    _leftBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 25)];
    [_leftBtn setTitle:@"left" forState:UIControlStateNormal];
    //    [leftBtn setBackgroundImage:[UIImage imageNamed:@"lift"] forState:UIControlStateNormal];
    [_leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_leftBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftItem=[[UIBarButtonItem alloc]initWithCustomView:_leftBtn];
    self.navigationItem.leftBarButtonItem=leftItem;
    
    _rightBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 25)];
    [_rightBtn setTitle:@"right" forState:UIControlStateNormal];
    [_rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    //    [rightBtn setBackgroundImage:[UIImage imageNamed:@"rig"] forState:UIControlStateNormal];
    
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithCustomView:_rightBtn];
    self.navigationItem.rightBarButtonItem=rightItem;
    _leftBtn.hidden = YES;
    _rightBtn.hidden = YES;
}
-(void)leftBtnAction
{
    NSLog(@"leftClick");
}
- (void)setTableView:(UITableView *)tableView{
    _tableView = tableView;
}
- (void)setHeadImageView:(UIImageView *)headImageView{
    _headImageView = headImageView;
}
- (void)setNameLabel:(UILabel *)nameLabel{
    _nameLabel = nameLabel;
}
- (void)setImageBG:(UIImageView *)imageBG{
    _imageBG = imageBG;
}

-(void)rightBtnAction
{
    NSLog(@"rightClick");
}

#pragma mark - tableViewDelegate&DataSource


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titlesArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        
    }
    
    cell.textLabel.text = self.titlesArray[indexPath.row];
    if (indexPath.row == 0) {
        self.noticeSwitch = [[UISwitch alloc] init];
        self.noticeSwitch.onTintColor = [UIColor greenColor];
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"enablePush"] != nil) {
            BOOL enablePush = [[[NSUserDefaults standardUserDefaults] objectForKey:@"enablePush"] boolValue];
            self.noticeSwitch.on = enablePush;
            if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"enablePush"] isEqual:@(YES)]) {
                [[UIApplication sharedApplication] registerForRemoteNotifications];
                [LKPushSetModel setUpMyPushInfo];
            }else{
                [[UIApplication sharedApplication] unregisterForRemoteNotifications];
                [LKPushSetModel clearPushInfo];
            }
        }else{
           self.noticeSwitch.on = [self isAllowedNotification];
        }
        
        [self.noticeSwitch addTarget:self action:@selector(noticeOnOrNo:) forControlEvents:UIControlEventValueChanged];
        [cell.contentView addSubview:self.noticeSwitch];
        [self.noticeSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView);
            make.right.equalTo(cell.contentView.mas_right).offset(-10);
        }];
        return cell;
    }
    if (indexPath.row == 1) {
        self.showHouseSwitch = [[UISwitch alloc] init];
        self.showHouseSwitch.onTintColor = [UIColor greenColor];
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"showHouse"] != nil) {
            BOOL showHouse = [[[NSUserDefaults standardUserDefaults] objectForKey:@"showHouse"] boolValue];
            self.showHouseSwitch.on = showHouse;
        }else{
            self.showHouseSwitch.on = YES;
        }
        [self.showHouseSwitch addTarget:self action:@selector(showHouserOrNot:) forControlEvents:UIControlEventValueChanged];
        [cell.contentView addSubview:self.showHouseSwitch];
        [self.showHouseSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView);
            make.right.equalTo(cell.contentView.mas_right).offset(-10);
        }];
        return cell;
    }
    return cell;
}

<<<<<<< HEAD
/**
 设置推送的关闭和开启按钮

 @param noticeSwitch 推送的开合
 */
-(void)noticeOnOrNo:(UISwitch *)noticeSwitch{
    if (noticeSwitch.on) {
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        [LKPushSetModel setUpMyPushInfo];
        [[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:@"enablePush"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }else{
        [[UIApplication sharedApplication] unregisterForRemoteNotifications];
        [LKPushSetModel clearPushInfo];
        [[NSUserDefaults standardUserDefaults] setObject:@(NO) forKey:@"enablePush"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
    }


}

/**
 是否在地图上显示机房

 @param showHouseSwithc 地图上是否显示机房的开关
 */
-(void)showHouserOrNot:(UISwitch *)showHouseSwithc{

    if (showHouseSwithc.on) {
        [[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:@"showHouse"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:@(NO) forKey:@"showHouse"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:@"showHouseActionNotice" object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showHomeHouseActionNotice" object:nil];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.titlesArray.count - 1) {
        [LKUserInforTool noAccount];
        [LKSwitchRootVcTool switchRootController];
    }
    if (indexPath.row == 2) {
        LKResetSecrectViewController *vcScrect = [[LKResetSecrectViewController alloc] initWithNibName:@"LKResetSecrectViewController" bundle:nil];
        vcScrect.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vcScrect animated:YES];
    }
}


=======
>>>>>>> parent of 2e14138... 提交
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat yOffset  = scrollView.contentOffset.y;
    CGFloat xOffset = (yOffset + BackGroupHeight)/2;
    
    if (yOffset < -BackGroupHeight) {
        
        CGRect rect = _imageBG.frame;
        rect.origin.y = yOffset;
        rect.size.height =  -yOffset ;
        rect.origin.x = xOffset;
        rect.size.width = WIDTH + fabs(xOffset)*2;
        _imageBG.frame = rect;
        CGRect HeadImageRect = CGRectMake((WIDTH-HeadImageHeight)/2, 40, HeadImageHeight, HeadImageHeight);
        HeadImageRect.origin.y = _headImageView.frame.origin.y;
        HeadImageRect.size.height =  HeadImageHeight + fabs(xOffset)*0.5 ;
        HeadImageRect.origin.x = self.view.center.x - HeadImageRect.size.height/2;
        HeadImageRect.size.width = HeadImageHeight + fabs(xOffset)*0.5;
        _headImageView.frame = HeadImageRect;
        _headImageView.layer.cornerRadius = HeadImageRect.size.height/2;
        _headImageView.clipsToBounds = YES;
        
        CGRect NameRect = CGRectMake((WIDTH-HeadImageHeight)/2, CGRectGetMaxY(_headImageView.frame)+5, HeadImageHeight, 20);
        NameRect.origin.y = CGRectGetMaxY(_headImageView.frame)+5;
        NameRect.size.height =  20 + fabs(xOffset)*0.5 ;
        NameRect.size.width = HeadImageHeight + fabs(xOffset)*0.5;
        NameRect.origin.x = self.view.center.x - NameRect.size.width/2;
        
        _nameLabel.font=[UIFont systemFontOfSize:17+fabs(xOffset)*0.2];
        
        _nameLabel.frame = NameRect;
        
        
    }else{
        CGRect HeadImageRect = CGRectMake((WIDTH-HeadImageHeight)/2, 40, HeadImageHeight, HeadImageHeight);
        HeadImageRect.origin.y = _headImageView.frame.origin.y;
        HeadImageRect.size.height =  HeadImageHeight - fabs(xOffset)*0.5 ;
        HeadImageRect.origin.x = self.view.center.x - HeadImageRect.size.height/2;
        HeadImageRect.size.width = HeadImageHeight - fabs(xOffset)*0.5;
        _headImageView.frame = HeadImageRect;
        _headImageView.layer.cornerRadius = HeadImageRect.size.height/2;
        _headImageView.clipsToBounds = YES;
        
        CGRect NameRect = CGRectMake((WIDTH-HeadImageHeight)/2, CGRectGetMaxY(_headImageView.frame)+5, HeadImageHeight, 20);
        NameRect.origin.y = CGRectGetMaxY(_headImageView.frame)+5;
        NameRect.size.height =  20;
        NameRect.size.width = HeadImageHeight - fabs(xOffset)*0.5;
        NameRect.origin.x = self.view.center.x - NameRect.size.width/2;
        
        _nameLabel.font=[UIFont systemFontOfSize:17-fabs(xOffset)*0.2];
        
        _nameLabel.frame = NameRect;
        
    }
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"BGimage.jpg"] forBarMetrics:UIBarMetricsDefault];
//    CGFloat alpha = 0;
//    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor clearColor]colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];

    
}


- (UIImage *)imageWithColor:(UIColor *)color
{
    // 描述矩形
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    
    // 开启位图上下文
    UIGraphicsBeginImageContext(rect.size);
    // 获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    // 渲染上下文
    CGContextFillRect(context, rect);
    // 从上下文中获取图片
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    // 结束上下文
    UIGraphicsEndImageContext();
    
    return theImage;
}

-(NSArray *)titlesArray{
    if (!_titlesArray) {
        _titlesArray = @[@"消息推送",@"地图显示机房",@"修改密码",@"退出登录"];
    }
    return _titlesArray;
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)dealloc{
    

    NSLog(@"释放了");
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
